<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="block本质是什么? block 本质上也是一个OC对象,内部也有个isa指针 block是封装了函数调用以及函数调用环境的OC对象(说的是block内部用外部的变量)">
<meta property="og:type" content="article">
<meta property="og:title" content="Block">
<meta property="og:url" content="http://yoursite.com/2018/06/16/Block/index.html">
<meta property="og:site_name" content="Erhqe">
<meta property="og:description" content="block本质是什么? block 本质上也是一个OC对象,内部也有个isa指针 block是封装了函数调用以及函数调用环境的OC对象(说的是block内部用外部的变量)">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1ftpjsbz0omj30ge06r0u3.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/006tKfTcly1ftpjtf7u0rj30g20eftbx.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tNc79ly1fsd7m7gnpoj30lq04775b.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tKfTcly1ftpnkm23mdj30i80a9tal.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tNc79ly1fse0uewi0kj30jm09s0vn.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/large/006tKfTcly1ftpuy7wj38j30h203o75m.jpg">
<meta property="og:image" content="https://ws3.sinaimg.cn/large/006tNc79ly1ftpxdvb7hbj30ir06ptaq.jpg">
<meta property="og:image" content="https://ws4.sinaimg.cn/large/006tNc79ly1ftpxnk77h0j30h00dignq.jpg">
<meta property="og:image" content="https://ws2.sinaimg.cn/large/006tNc79ly1ftpy8ty5zzj30uz0amwit.jpg">
<meta property="og:image" content="https://ws1.sinaimg.cn/large/006tNc79ly1ftpyjmg10kj30ie0djdih.jpg">
<meta property="og:updated_time" content="2019-07-19T05:40:06.071Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Block">
<meta name="twitter:description" content="block本质是什么? block 本质上也是一个OC对象,内部也有个isa指针 block是封装了函数调用以及函数调用环境的OC对象(说的是block内部用外部的变量)">
<meta name="twitter:image" content="https://ws2.sinaimg.cn/large/006tKfTcly1ftpjsbz0omj30ge06r0u3.jpg">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: 'Author'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/06/16/Block/"/>





  <title>Block | Erhqe</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Erhqe</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            Home
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            Categories
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            Archives
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/06/16/Block/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Erhqe">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/10.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Erhqe">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Block</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              
              <time title="Post created" itemprop="dateCreated datePublished" datetime="2018-06-16T16:50:28+08:00">
                2018-06-16
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/InterView/" itemprop="url" rel="index">
                    <span itemprop="name">InterView</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2018/06/16/Block/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2018/06/16/Block/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p><strong>block本质是什么?</strong></p>
<p><strong>block 本质上也是一个OC对象,内部也有个isa指针</strong></p>
<p><strong>block是封装了函数调用以及函数调用环境的OC对象(说的是block内部用外部的变量)</strong><a id="more"></a></p>
<p><strong>转成C++之后的图</strong></p>
<figure class="highlight ruby"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m </span><br><span class="line"></span><br><span class="line">__weak,或者__strong 修饰时一下边的转成C++,因为__weak和strong是需要支持runtime的</span><br><span class="line">xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-<span class="number">8.0</span>.<span class="number">0</span> main.m</span><br></pre></td></tr></table></figure>
<p><strong>block源码查看</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在项目中</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        <span class="keyword">int</span> age = <span class="number">10</span>;</span><br><span class="line">        <span class="keyword">void</span>(^block)(<span class="keyword">int</span> ,<span class="keyword">int</span>) = ^(<span class="keyword">int</span> a ,<span class="keyword">int</span> b)&#123;</span><br><span class="line">            NSLog(@<span class="string">"AA%d"</span>,age);</span><br><span class="line">            NSLog(@<span class="string">"AA"</span>);</span><br><span class="line">            NSLog(@<span class="string">"AA"</span>);</span><br><span class="line">        &#125;;</span><br><span class="line">        block(<span class="number">10</span>,<span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//转成C++之后</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//block 第一个参数 </span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> &#123;</span></span><br><span class="line"> <span class="keyword">void</span> *isa; <span class="comment">//证明他是个OC对象</span></span><br><span class="line"> <span class="keyword">int</span> Flags;</span><br><span class="line"> <span class="keyword">int</span> Reserved;</span><br><span class="line"> <span class="keyword">void</span> *FuncPtr;</span><br><span class="line"> &#125;;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// block描述结构体</span></span><br><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span> &#123;</span></span><br><span class="line">  <span class="keyword">size_t</span> reserved;</span><br><span class="line">  <span class="keyword">size_t</span> Block_size;</span><br><span class="line">&#125; __main_block_desc_0_DATA = &#123; <span class="number">0</span>, <span class="keyword">sizeof</span>(struct __main_block_impl_0)&#125;;<span class="comment">//这一行可以看出,就是block的本身的大小,相当于计算block这个结构体多大</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// block本身</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> &#123;</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> <span class="title">impl</span>;</span>	<span class="comment">// block的第一个参数是一个结构体在上面</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span>* <span class="title">Desc</span>;</span></span><br><span class="line"> <span class="keyword">int</span> age;</span><br><span class="line">     <span class="comment">//3__main_block_desc_0_DATA 传到FuncPtr中</span></span><br><span class="line">     <span class="comment">// 类似于OC的构造函数</span></span><br><span class="line"> __main_block_impl_0(<span class="keyword">void</span> *fp, struct __main_block_desc_0 *desc, <span class="keyword">int</span> _age, <span class="keyword">int</span> flags=<span class="number">0</span>) : age(_age) &#123;</span><br><span class="line"> impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line"> impl.Flags = flags;</span><br><span class="line"> impl.FuncPtr = fp;</span><br><span class="line"> Desc = desc;</span><br><span class="line"> &#125;</span><br><span class="line"> &#125;;</span><br><span class="line"> </span><br><span class="line"></span><br><span class="line"> <span class="comment">// block调用封装的函数,传到block定义中 1</span></span><br><span class="line"> <span class="keyword">static</span> <span class="keyword">void</span> __main_block_func_0(struct __main_block_impl_0 *__cself, <span class="keyword">int</span> a, <span class="keyword">int</span> b) &#123;</span><br><span class="line"> <span class="keyword">int</span> age = __cself-&gt;age; <span class="comment">// bound by copy</span></span><br><span class="line"> </span><br><span class="line"> NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_1b19fa_mi_0,age);</span><br><span class="line"> NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_1b19fa_mi_1);</span><br><span class="line"> NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_1b19fa_mi_2);</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 项目中的age</span></span><br><span class="line"> <span class="keyword">int</span> age = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 项目中的Block定义,</span></span><br><span class="line"><span class="comment">// __main_block_func_0 2, 传到 block本身的第四个参数(第一个参数),第二个参数block描述结构体</span></span><br><span class="line"> <span class="keyword">void</span>(*block)(<span class="keyword">int</span> ,<span class="keyword">int</span>) = ((<span class="keyword">void</span> (*)(<span class="keyword">int</span>, <span class="keyword">int</span>))&amp;__main_block_impl_0((<span class="keyword">void</span> *)__main_block_func_0, &amp;__main_block_desc_0_DATA, age));</span><br><span class="line"> </span><br><span class="line">  <span class="comment">// 项目中的Block调用</span></span><br><span class="line"><span class="comment">// 4 利用FuncPtr调用block封装的函数</span></span><br><span class="line"> ((<span class="keyword">void</span> (*)(__block_impl *, <span class="keyword">int</span>, <span class="keyword">int</span>))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block, <span class="number">10</span>, <span class="number">10</span>);</span><br></pre></td></tr></table></figure>
<p><strong>证明上面的解释</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 我们自己定义Block</span></span><br><span class="line"><span class="comment">// 将自己Block转成定义的结构体</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> &#123;</span></span><br><span class="line">    <span class="keyword">void</span> *isa;</span><br><span class="line">    <span class="keyword">int</span> Flags;</span><br><span class="line">    <span class="keyword">int</span> Reserved;</span><br><span class="line">    <span class="keyword">void</span> *FuncPtr;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// block描述</span></span><br><span class="line"> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span> &#123;</span></span><br><span class="line">    <span class="keyword">size_t</span> reserved; <span class="comment">//保留的</span></span><br><span class="line">    <span class="keyword">size_t</span> Block_size;	<span class="comment">// block 大小</span></span><br><span class="line"> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> &#123;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> <span class="title">impl</span>;</span></span><br><span class="line">    <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span>* <span class="title">Desc</span>;</span></span><br><span class="line">    <span class="keyword">int</span> age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        <span class="keyword">int</span> age = <span class="number">20</span>;</span><br><span class="line">        <span class="keyword">void</span>(^block)(<span class="keyword">int</span> ,<span class="keyword">int</span>) = ^(<span class="keyword">int</span> a ,<span class="keyword">int</span> b)&#123;</span><br><span class="line">            NSLog(@<span class="string">"AA%d"</span>,age);</span><br><span class="line">            NSLog(@<span class="string">"AA"</span>);</span><br><span class="line">            NSLog(@<span class="string">"AA"</span>);</span><br><span class="line">        &#125;;</span><br><span class="line">        <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> *<span class="title">blockStruc</span> = (__<span class="title">bridge</span>  <span class="title">struct</span> __<span class="title">main_block_impl_0</span> *)<span class="title">block</span>;</span></span><br><span class="line">        </span><br><span class="line">        block(<span class="number">10</span>,<span class="number">10</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>看图,首先看age=20,block会将外面的age=20拿去,</strong></p>
<p><img src="https://ws2.sinaimg.cn/large/006tKfTcly1ftpjsbz0omj30ge06r0u3.jpg" alt=""></p>
<p><strong>在block调用时打断点NSLog(@”AA”),再看上面的FuncPtr的地址,正式下方的地址,证明block调用的时候,确实是调用FuncPtr的</strong></p>
<p><img src="https://ws4.sinaimg.cn/large/006tKfTcly1ftpjtf7u0rj30g20eftbx.jpg" alt=""></p>
<h5 id="变量捕获"><a href="#变量捕获" class="headerlink" title="变量捕获"></a>变量捕获</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">// 为什么打印结果是10 ?,而不是20?</span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        </span><br><span class="line">        int age = 10;</span><br><span class="line">        void (^block)(void) = ^&#123;</span><br><span class="line">            NSLog(@&quot;hello word--%d&quot;,age);</span><br><span class="line">        &#125;;</span><br><span class="line">        </span><br><span class="line">        age = 20;</span><br><span class="line">        block();</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>分析C++代码-auto变量</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">int</span> age = <span class="number">10</span>;</span><br><span class="line"><span class="comment">//	block定义</span></span><br><span class="line">        <span class="keyword">void</span> (*block)(<span class="keyword">void</span>) = ((<span class="keyword">void</span> (*)())&amp;__main_block_impl_0((<span class="keyword">void</span> *)__main_block_func_0, &amp;__main_block_desc_0_DATA, age));</span><br><span class="line"></span><br><span class="line">        age = <span class="number">20</span>;</span><br><span class="line"><span class="comment">//	block调用</span></span><br><span class="line">        ((<span class="keyword">void</span> (*)(__block_impl *))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block);</span><br><span class="line"><span class="comment">//从上面看来 修改age的值之前将age的(值)传递到参数里面去了,而不是地址,所以不会发生改变</span></span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment">//block本身</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> &#123;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> <span class="title">impl</span>;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span>* <span class="title">Desc</span>;</span></span><br><span class="line">  <span class="keyword">int</span> age;	<span class="comment">// 内部新增一个成员这叫	变量捕获</span></span><br><span class="line">  __main_block_impl_0(<span class="keyword">void</span> *fp, struct __main_block_desc_0 *desc, <span class="keyword">int</span> _age, <span class="keyword">int</span> flags=<span class="number">0</span>) : age(_age) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//: age(_age) C++语法:意思是将_age赋值给成员变量int age;</span></span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">    </span><br><span class="line"><span class="comment">//block封装的调用函数,block调用:((void (*)(__block_impl *))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block); 调用会走到下边的函数</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_func_0(struct __main_block_impl_0 *__cself) &#123;</span><br><span class="line">  <span class="keyword">int</span> age = __cself-&gt;age; <span class="comment">// 取出Age打印</span></span><br><span class="line"></span><br><span class="line">            NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_5aa1b9_mi_0,age);</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<p><strong>分析C++代码-Static变量</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">// 为什么是20?</span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        </span><br><span class="line">       static int age = 10;</span><br><span class="line">        void (^block)(void) = ^&#123;</span><br><span class="line">            NSLog(@&quot;hello word--%d&quot;,age);</span><br><span class="line">        &#125;;</span><br><span class="line">        </span><br><span class="line">        age = 20;</span><br><span class="line">        block();</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">    </span><br><span class="line">// 看源代码</span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    /* @autoreleasepool */ &#123; __AtAutoreleasePool __autoreleasepool; </span><br><span class="line"></span><br><span class="line">       static int age = 10;</span><br><span class="line">        void (*block)(void) = ((void (*)())&amp;__main_block_impl_0((void *)__main_block_func_0, &amp;__main_block_desc_0_DATA, &amp;age));	// 地址传递所以打印结果为20</span><br><span class="line"></span><br><span class="line">        age = 20;</span><br><span class="line">        ((void (*)(__block_impl *))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">//block本身</span><br><span class="line">struct __main_block_impl_0 &#123;</span><br><span class="line">  struct __block_impl impl;</span><br><span class="line">  struct __main_block_desc_0* Desc;</span><br><span class="line">  int *age;	//捕获地址</span><br><span class="line">  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int *_age, int flags=0) : age(_age) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">//block函数</span><br><span class="line">static void __main_block_func_0(struct __main_block_impl_0 *__cself) &#123;</span><br><span class="line">  int *age = __cself-&gt;age; // 指针取值</span><br><span class="line"></span><br><span class="line">            NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_0c016d_mi_0,(*age));</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<p><strong>分析C++代码-全局变量</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> age_ = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">int</span> height_ = <span class="number">10</span>;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        <span class="keyword">void</span> (^block)(<span class="keyword">void</span>) = ^&#123;</span><br><span class="line">            NSLog(@<span class="string">"hello word--%d--%d"</span>,age_,height_);</span><br><span class="line">        &#125;;</span><br><span class="line">        age_ = <span class="number">20</span>;</span><br><span class="line">        height_ = <span class="number">20</span>;</span><br><span class="line">        block();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">    </span><br><span class="line"><span class="comment">//发现外部定义了全局变量直接访问,block内部没有捕获变量</span></span><br><span class="line"><span class="keyword">int</span> age_ = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">static</span> <span class="keyword">int</span> height_ = <span class="number">10</span>;	</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> &#123;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> <span class="title">impl</span>;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span>* <span class="title">Desc</span>;</span></span><br><span class="line">  __main_block_impl_0(<span class="keyword">void</span> *fp, struct __main_block_desc_0 *desc, <span class="keyword">int</span> flags=<span class="number">0</span>) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">    </span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_func_0(struct __main_block_impl_0 *__cself) &#123;</span><br><span class="line"></span><br><span class="line">            NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_40bc90_mi_0,age_,height_);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span> &#123;</span></span><br><span class="line">  <span class="keyword">size_t</span> reserved;</span><br><span class="line">  <span class="keyword">size_t</span> Block_size;</span><br><span class="line">&#125; __main_block_desc_0_DATA = &#123; <span class="number">0</span>, <span class="keyword">sizeof</span>(struct __main_block_impl_0)&#125;;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    <span class="comment">/* @autoreleasepool */</span> &#123; __AtAutoreleasePool __autoreleasepool; </span><br><span class="line">        <span class="keyword">void</span> (*block)(<span class="keyword">void</span>) = ((<span class="keyword">void</span> (*)())&amp;__main_block_impl_0((<span class="keyword">void</span> *)__main_block_func_0, &amp;__main_block_desc_0_DATA));</span><br><span class="line">        age_ = <span class="number">20</span>;</span><br><span class="line">        height_ = <span class="number">20</span>;</span><br><span class="line">        ((<span class="keyword">void</span> (*)(__block_impl *))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>为什么auto变量时捕获值,static变量捕获地址呢?,全局变量不捕获</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">因为auto变量出了大括号就会销毁,所以捕获值</span><br><span class="line">因为static变量不会销毁所以捕获地址</span><br><span class="line">以为全局变量没必要捕获</span><br></pre></td></tr></table></figure>
<p><img src="https://ws2.sinaimg.cn/large/006tNc79ly1fsd7m7gnpoj30lq04775b.jpg" alt=""></p>
<p><strong>在看看另一种情况,会不会捕获?</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">@implementation Person</span><br><span class="line">- (void)test&#123;</span><br><span class="line">    void(^block)(void) = ^&#123;</span><br><span class="line">        NSLog(@&quot;%@&quot;,self);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br><span class="line"></span><br><span class="line">// 是捕获的,值传递,因为Self是个局部变量</span><br><span class="line">//因为test函数转换成C语言实数之后默认有两个参数</span><br><span class="line">//void test (Person *self,SEL _cmd)</span><br><span class="line"></span><br><span class="line">struct __Person__test_block_impl_0 &#123;</span><br><span class="line">  struct __block_impl impl;</span><br><span class="line">  struct __Person__test_block_desc_0* Desc;</span><br><span class="line">  Person *self;</span><br><span class="line">  __Person__test_block_impl_0(void *fp, struct __Person__test_block_desc_0 *desc, Person *_self, int flags=0) : self(_self) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">    </span><br><span class="line">// 如果这样的话,会不会捕获?,会捕获,但是捕获的Self,因为取name的时候,从Self取</span><br><span class="line">@implementation Person</span><br><span class="line">- (void)test&#123;</span><br><span class="line">    void(^block)(void) = ^&#123;  </span><br><span class="line">        NSLog(@&quot;%@&quot;,_name);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line">@end</span><br></pre></td></tr></table></figure>
<h5 id="Block类型"><a href="#Block类型" class="headerlink" title="Block类型"></a>Block类型</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    //	先把ARC关掉,因为ARC做了很多事情,会影响到打印结果,MRC的环境下打印</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        //	没有访问auto变量 NSGlobalBlock ,也就 在代码段</span><br><span class="line">        void (^block1)(void) = ^&#123;</span><br><span class="line">            NSLog(@&quot;Hello&quot;);</span><br><span class="line">        &#125;;</span><br><span class="line">        //	访问可auto变量 NSStackBlock ,也就是	因为auto变量在栈区(Stack),有问题栈区超出作用域他会自动销毁</span><br><span class="line">        int age = 10;</span><br><span class="line">        void (^block2)(void) = ^&#123;</span><br><span class="line">            NSLog(@&quot;%d&quot;,age);</span><br><span class="line">        &#125;;</span><br><span class="line">        //	NSStackBlock copy 之后会变成 NSMallocBlock,因为Copy会拷贝到堆区</span><br><span class="line">        NSLog(@&quot;%@ -- %@ -- %@ &quot;,[block1 class],[block2 class],[^&#123;</span><br><span class="line">            NSLog(@&quot;%d&quot;,age);</span><br><span class="line">        &#125; class ]);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line">// 打印结果为 __NSGlobalBlock__ -- __NSMallocBlock__ -- __NSStackBlock__ 因此Block有三种类型,这三种的父类NSBlock,而NSBlock的父类是NSObject,因此可以证明Block是OC对象,isa还可以证明也是OC对象,Block的isa是有NSObject来的</span><br></pre></td></tr></table></figure>
<p><strong>NSStackBlock销毁演示,及解决方法</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">//在MRC, ARC下是不会看到的,因为ARC下编译器会将栈上Block复制到堆上(一下情况会编译器会赋值block)</span><br><span class="line">1.block作为函数返回值,</span><br><span class="line">2.block被强指针指着</span><br><span class="line">3.blcok作为Cocoa API中方法名含有usingBlock</span><br><span class="line">4.block作为GCD API的方法参数时</span><br><span class="line">block在两种环境下写法</span><br><span class="line">    MRC: @property(copy,nonatomic)void(^block)();</span><br><span class="line">	ARC: @property(copy,nonatomic)void(^block)();</span><br><span class="line">	ARC: @property(strong,nonatomic)void(^block)();	//强制镇指着与会copy所以在ARC下都一样,规范ARC下Copy</span><br><span class="line">void test()&#123;</span><br><span class="line">    int age = 10;</span><br><span class="line">    block = ^&#123;</span><br><span class="line">        NSLog(@&quot;%d&quot;,age);</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        test();</span><br><span class="line">        block();</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line">//	打印结果不是10,而是很长的数字,因为block的内存被释放了,也就是说test函数一旦调用完毕,这个结构体就释放了</span><br><span class="line">struct __main_block_impl_0 &#123;</span><br><span class="line">  struct __block_impl impl;</span><br><span class="line">  struct __main_block_desc_0* Desc;</span><br><span class="line">  int age;	</span><br><span class="line">  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _age, int flags=0) : age(_age) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line">    </span><br><span class="line">//解决方法是Copy,一旦NSStackBlock进行Copy,就会变成NSMallocBlock,也就意味着程序员自己管理内存,引用计数加一的</span><br><span class="line">void test()&#123;</span><br><span class="line">    int age = 10;</span><br><span class="line">    block = [^&#123;</span><br><span class="line">        NSLog(@&quot;%d&quot;,age);</span><br><span class="line">    &#125;copy];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><strong>Block 对象类型的auto变量</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">//在ARC下</span><br><span class="line">//Person是不会被销毁的</span><br><span class="line">//因为ARC下编译器对block新型copy</span><br><span class="line">#import &quot;Person.h&quot;</span><br><span class="line">typedef void(^Block)(void);</span><br><span class="line"></span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        Block block;</span><br><span class="line">      //按理说出了大括号就行该销毁  </span><br><span class="line">    &#123;</span><br><span class="line">        Person *person = [[Person alloc]init];</span><br><span class="line">        person.name = @&quot;jack&quot;;</span><br><span class="line">        block = ^&#123;</span><br><span class="line">            NSLog(@&quot;%@&quot;,person.name);</span><br><span class="line">        &#125;;</span><br><span class="line">    &#125;</span><br><span class="line">        NSLog(@&quot;------&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">//在MRC下 出了大括号person就会被销毁但是加上copy之后不会销毁  [person retain]引用计数+1</span><br><span class="line">#import &quot;Person.h&quot;</span><br><span class="line">typedef void(^Block)(void);</span><br><span class="line"></span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        Block block;</span><br><span class="line">      //按理说出了大括号就行该销毁  </span><br><span class="line">    &#123;</span><br><span class="line">        Person *person = [[Person alloc]init];</span><br><span class="line">        person.name = @&quot;jack&quot;;</span><br><span class="line">        block = [^&#123;</span><br><span class="line">            NSLog(@&quot;%@&quot;,person.name);</span><br><span class="line">        &#125;copy];</span><br><span class="line">    &#125;</span><br><span class="line">        NSLog(@&quot;------&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<p><strong>block在内存中怎么样的</strong></p>
<p><img src="https://ws3.sinaimg.cn/large/006tKfTcly1ftpnkm23mdj30i80a9tal.jpg" alt=""></p>
<p><img src="https://ws2.sinaimg.cn/large/006tNc79ly1fse0uewi0kj30jm09s0vn.jpg" alt=""></p>
<p> <strong>Block对对象的引用方式</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//__weak,或者__strong 修饰时一下边的转成C++,因为__weak和strong是需要支持runtime的</span></span><br><span class="line"><span class="comment">//xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 main.m</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//ARC</span></span><br><span class="line"><span class="meta">#import <span class="meta-string">"Person.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">typedef</span> <span class="title">void</span><span class="params">(^Block)</span><span class="params">(<span class="keyword">void</span>)</span></span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        Block block;</span><br><span class="line">        &#123;</span><br><span class="line">            Person *p = [[Person alloc]init];</span><br><span class="line">            p.name = @<span class="string">"jack"</span>;</span><br><span class="line">            <span class="comment">// block被强引用着,编译器会进行copy</span></span><br><span class="line">            block = ^&#123;</span><br><span class="line">                NSLog(@<span class="string">"%@"</span>,p.name);</span><br><span class="line">            &#125;;</span><br><span class="line">            <span class="comment">/*</span></span><br><span class="line"><span class="comment">             // 如果这样没有强引用那就编译器是不会机型copy,栈上block</span></span><br><span class="line"><span class="comment">             // 记住一点只要block是在栈上,都不会外面的auto变量强引用</span></span><br><span class="line"><span class="comment">             ^&#123;</span></span><br><span class="line"><span class="comment">                NSLog(@"%@",p.name);</span></span><br><span class="line"><span class="comment">             &#125;;</span></span><br><span class="line"><span class="comment">             */</span></span><br><span class="line">        &#125;</span><br><span class="line">        NSLog(@<span class="string">"----------"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment">//C++之后,</span></span><br><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span> &#123;</span></span><br><span class="line">  <span class="keyword">size_t</span> reserved;</span><br><span class="line">  <span class="keyword">size_t</span> Block_size;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//发现多了两个函数指针,比如int a = 10;这个时候是没有下面这函数指针的,注意对象类型block,Copy操作才会多两个函数指针</span></span><br><span class="line">  <span class="keyword">void</span> (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);</span><br><span class="line">  <span class="keyword">void</span> (*dispose)(struct __main_block_impl_0*);</span><br><span class="line">    </span><br><span class="line">&#125; __main_block_desc_0_DATA = &#123;</span><br><span class="line"><span class="number">0</span>,</span><br><span class="line"><span class="keyword">sizeof</span>(struct __main_block_impl_0),</span><br><span class="line">__main_block_copy_0,</span><br><span class="line">__main_block_dispose_0&#125;;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这个函数是上面的第三个参数</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) &#123;</span><br><span class="line">    _Block_object_assign((<span class="keyword">void</span>*)&amp;dst-&gt;p, (<span class="keyword">void</span>*)src-&gt;p, <span class="number">3</span><span class="comment">/*BLOCK_FIELD_IS_OBJECT*/</span>);</span><br><span class="line">    <span class="comment">//这个函数会根据下面的结构体中修饰,来 引用对象类型的auto变量</span></span><br><span class="line">    <span class="comment">//如果是Person *__strong p;这样那么强引用</span></span><br><span class="line">    <span class="comment">//如果是Person *__weak p;弱引用</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">---------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="comment">// Block本身</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> &#123;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> <span class="title">impl</span>;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span>* <span class="title">Desc</span>;</span></span><br><span class="line">  Person *__strong p;</span><br><span class="line">  __main_block_impl_0(<span class="keyword">void</span> *fp, struct __main_block_desc_0 *desc, Person *__strong _p, <span class="keyword">int</span> flags=<span class="number">0</span>) : p(_p) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 只要block是在栈上,都不会外面的auto变量强引用</span></span><br><span class="line"><span class="comment">// 如果block拷贝到堆上那么block内部会调用_Block_object_assign函数</span></span><br><span class="line"><span class="comment">// _Block_object_assign函数根据auto变量的修饰符(__weak __strong __unsafe_unretain),对autobianl做出相应的操作</span></span><br><span class="line"><span class="comment">// 一旦block销毁时(ARC也会销毁么)内部会调用__main_block_dispose_0函数,</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_dispose_0(struct __main_block_impl_0*src) &#123;</span><br><span class="line">    _Block_object_dispose((<span class="keyword">void</span>*)src-&gt;p, <span class="number">3</span><span class="comment">/*BLOCK_FIELD_IS_OBJECT*/</span>);</span><br><span class="line">    <span class="comment">// 这个函数会自动释放应用的auto变量,类似于release,就是block对person的引用没了,而不是Person直接销毁了,如果之前person引用指数是1的话那么_Block_object_dispose之后person肯定挂了</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://ws1.sinaimg.cn/large/006tKfTcly1ftpuy7wj38j30h203o75m.jpg" alt=""></p>
<p><strong>释放时机观察</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">- (void)touchesBegan:(NSSet&lt;UITouch *&gt; *)touches withEvent:(UIEvent *)event &#123;</span><br><span class="line">    [super touchesBegan:touches withEvent:event];</span><br><span class="line">    NSLog(@&quot;touchesBegan -- &quot;);</span><br><span class="line">    Person *p = [[Person alloc]init];</span><br><span class="line">    __weak Person *weakP = p;</span><br><span class="line">    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^&#123;</span><br><span class="line">        NSLog(@&quot;%@&quot;,weakP);</span><br><span class="line">        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^&#123;</span><br><span class="line">            NSLog(@&quot;%@&quot;,p);</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line">/*</span><br><span class="line">15:18:07.833784+0800 删除Block[1661:148061] touchesBegan --</span><br><span class="line">15:18:08.834259+0800 删除Block[1661:148061] &lt;Person: 0x60800000c5c0&gt;</span><br><span class="line">15:18:11.025798+0800 删除Block[1661:148061] &lt;Person: 0x60800000c5c0&gt;</span><br><span class="line">15:18:11.026043+0800 删除Block[1661:148061] Person - dealloc</span><br><span class="line">强引用3秒后释放</span><br><span class="line">不要看第一次弱引用,关键是里面是绝对有强引用的</span><br><span class="line">*/</span><br></pre></td></tr></table></figure>
<h5 id="Block基本数据类型"><a href="#Block基本数据类型" class="headerlink" title="__Block基本数据类型"></a>__Block基本数据类型</h5><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br></pre></td><td class="code"><pre><span class="line">// block调用时想修改age的值</span><br><span class="line">// 方法1static,方法二全局变量(这两个方弊端就是age一直在内存中),所以__block就出来了,__block不会对变量的本质发生变化,还是auto变量</span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">       static int age = 10;</span><br><span class="line">        Block block = ^&#123;</span><br><span class="line">            age = 20;</span><br><span class="line">            NSLog(@&quot;age = %d&quot;,age);</span><br><span class="line">        &#125;;</span><br><span class="line">        block();</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">------------------------------------------------</span><br><span class="line"></span><br><span class="line">//C++之后</span><br><span class="line">//没有被__block修饰时</span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    /* @autoreleasepool */ &#123; __AtAutoreleasePool __autoreleasepool;</span><br><span class="line">       // 之前不用__block修饰时这下边的代码会这样</span><br><span class="line">		int age = 10;这样</span><br><span class="line">       </span><br><span class="line">        //block定义</span><br><span class="line">        Block block = ((void (*)())&amp;__main_block_impl_0((void *)__main_block_func_0, &amp;__main_block_desc_0_DATA, (__Block_byref_age_0 *)&amp;age, 570425344));</span><br><span class="line">        </span><br><span class="line">	//block调用</span><br><span class="line">        ((void (*)(__block_impl *))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">------------------------------------------------</span><br><span class="line"></span><br><span class="line">//被__block修饰时</span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    /* @autoreleasepool */ &#123; __AtAutoreleasePool __autoreleasepool;</span><br><span class="line">        </span><br><span class="line">		//age 发生了改变</span><br><span class="line">		//根据age类型的结构体赋值</span><br><span class="line">       __attribute__((__blocks__(byref))) __Block_byref_age_0 age = &#123;</span><br><span class="line">           (void*)0,	//__isa;</span><br><span class="line">           (__Block_byref_age_0 *)&amp;age,	//__Block_byref_age_0 *__forwarding;</span><br><span class="line">           0, //int __flags;</span><br><span class="line">           sizeof(__Block_byref_age_0),	//int __size;</span><br><span class="line">           10	// int age;</span><br><span class="line">       &#125;;</span><br><span class="line">        </span><br><span class="line">        Block block = ((void (*)())&amp;__main_block_impl_0((void *)__main_block_func_0, &amp;__main_block_desc_0_DATA, (__Block_byref_age_0 *)&amp;age, 570425344));</span><br><span class="line">        </span><br><span class="line">        ((void (*)(__block_impl *))((__block_impl *)block)-&gt;FuncPtr)((__block_impl *)block);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">------------------------------------------------</span><br><span class="line">//block本身</span><br><span class="line">struct __main_block_impl_0 &#123;</span><br><span class="line">  struct __block_impl impl;</span><br><span class="line">  struct __main_block_desc_0* Desc;</span><br><span class="line">    </span><br><span class="line">  __Block_byref_age_0 *age; // age变成了结构体,在下边</span><br><span class="line">    </span><br><span class="line">  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, __Block_byref_age_0 *_age, int flags=0) : age(_age-&gt;__forwarding) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">------------------------------------------------</span><br><span class="line">//age的类型</span><br><span class="line">struct __Block_byref_age_0 &#123;</span><br><span class="line">  void *__isa;	// 发现age变成了OC对象</span><br><span class="line">__Block_byref_age_0 *__forwarding;	// 从上面赋值来看,这是就是自己,保存着自己(整个结构体)</span><br><span class="line"> int __flags;</span><br><span class="line"> int __size;</span><br><span class="line"> int age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">------------------------------------------------</span><br><span class="line">//block 定义中可以看到调用这个函数修改的age的值</span><br><span class="line">static void __main_block_func_0(struct __main_block_impl_0 *__cself) &#123;</span><br><span class="line">  __Block_byref_age_0 *age = __cself-&gt;age; // bound by ref</span><br><span class="line"></span><br><span class="line">            (age-&gt;__forwarding-&gt;age) = 20;	//通过结构体指针修改为20</span><br><span class="line">            NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_d79e44_mi_0,(age-&gt;__forwarding-&gt;age));</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure>
<p><strong>C++结构体中的age是否是外面的age?</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">#import &lt;Foundation/Foundation.h&gt;</span><br><span class="line">#import &quot;Person.h&quot;</span><br><span class="line"></span><br><span class="line">typedef void(^Block)(void);</span><br><span class="line"></span><br><span class="line">struct __block_impl &#123;</span><br><span class="line">    void *isa;</span><br><span class="line">    int Flags;</span><br><span class="line">    int Reserved;</span><br><span class="line">    void *FuncPtr;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">struct __Block_byref_age_0 &#123;</span><br><span class="line">    void *__isa;</span><br><span class="line">    struct __Block_byref_age_0 *__forwarding;</span><br><span class="line">    int __flags;</span><br><span class="line">    int __size;</span><br><span class="line">    int age;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">struct __main_block_impl_0 &#123;</span><br><span class="line">    struct __block_impl impl;</span><br><span class="line">    struct __main_block_desc_0* Desc;</span><br><span class="line">    </span><br><span class="line">    struct __Block_byref_age_0 *age; // by ref</span><br><span class="line">    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">int main(int argc, const char * argv[]) &#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">       __block int age = 10;</span><br><span class="line">        Block block = ^&#123;</span><br><span class="line">            age = 20;</span><br><span class="line">            NSLog(@&quot;age = %d&quot;,age);</span><br><span class="line">        &#125;;</span><br><span class="line">        block();</span><br><span class="line">        struct __main_block_impl_0 *blockImpl = (__bridge struct __main_block_impl_0 *)block;</span><br><span class="line">        NSLog(@&quot;%p&quot;,&amp;age);</span><br><span class="line">        NSLog(@&quot;=====&quot;);</span><br><span class="line">    &#125;</span><br><span class="line">    return 0;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://ws3.sinaimg.cn/large/006tNc79ly1ftpxdvb7hbj30ir06ptaq.jpg" alt=""></p>
<p><strong>发现age那个结构体地址是0x102800850,外面打印age的地址是0x102800868 ,下边通过结构体的地址计算出结构体内的age地址</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">isa地址=<span class="number">0x102800850</span></span><br><span class="line">struct __Block_byref_age_0 &#123;</span><br><span class="line">  <span class="keyword">void</span> *__isa;	<span class="comment">//8个字节</span></span><br><span class="line">__Block_byref_age_0 *__forwarding;	<span class="comment">//8个字节 0x102800858</span></span><br><span class="line"> <span class="keyword">int</span> __flags; <span class="comment">//4个字节 0x102800860</span></span><br><span class="line"> <span class="keyword">int</span> __size; <span class="comment">//4个字节  0x102800864</span></span><br><span class="line"> <span class="keyword">int</span> age;	<span class="number">0x102800868</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//正好证明结构体里面的age和外面的age是同一个</span></span><br></pre></td></tr></table></figure>
<p><strong>也可以这样证明</strong></p>
<p><img src="https://ws4.sinaimg.cn/large/006tNc79ly1ftpxnk77h0j30h00dignq.jpg" alt=""></p>
<p><strong>__block的内存管理</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//发现 age被__block修饰之后,它的内部结构体描述结构体,也发生了变化</span></span><br><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span> &#123;</span></span><br><span class="line">  <span class="keyword">size_t</span> reserved;</span><br><span class="line">  <span class="keyword">size_t</span> Block_size;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//也是在这里多了两个函数指针</span></span><br><span class="line">  <span class="keyword">void</span> (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);</span><br><span class="line">  <span class="keyword">void</span> (*dispose)(struct __main_block_impl_0*);</span><br><span class="line">&#125; __main_block_desc_0_DATA = &#123;</span><br><span class="line">    <span class="number">0</span>,</span><br><span class="line">    <span class="keyword">sizeof</span>(struct __main_block_impl_0),</span><br><span class="line">    __main_block_copy_0,</span><br><span class="line">    __main_block_dispose_0</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">//因此__block内存管理,在这个函数决定强怎么引用(如果是__block int age = 10;这种就直接强引用)__main_block_copy_0</span></span><br><span class="line"><span class="comment">//因此__block内存管理,堆中移除时在这个函数释放__main_block_dispose_0</span></span><br></pre></td></tr></table></figure>
<p><img src="https://ws2.sinaimg.cn/large/006tNc79ly1ftpy8ty5zzj30uz0amwit.jpg" alt=""></p>
<p><strong>Forwarding</strong></p>
<p><img src="https://ws1.sinaimg.cn/large/006tNc79ly1ftpyjmg10kj30ie0djdih.jpg" alt=""></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//发现修改age的时候是通过下边的方式取的,</span></span><br><span class="line">forwarding定作用就是上面图,如果block被复制到堆上的话forwarding会指向堆上的地址,用意就是block不管是在栈或者堆都能获取到age</span><br><span class="line">(age-&gt;__forwarding-&gt;age) = <span class="number">20</span>;</span><br></pre></td></tr></table></figure>
<h5 id="Block对象类型"><a href="#Block对象类型" class="headerlink" title="__Block对象类型"></a>__Block对象类型</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    @autoreleasepool &#123;</span><br><span class="line">        __block __strong Person *p = [[Person alloc]init];</span><br><span class="line">        Block block = ^&#123;</span><br><span class="line">            </span><br><span class="line">            NSLog(@<span class="string">"%@"</span>,p);</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//C++</span></span><br><span class="line"><span class="comment">//block 本身</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">main_block_impl_0</span> &#123;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">block_impl</span> <span class="title">impl</span>;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span>* <span class="title">Desc</span>;</span> </span><br><span class="line">    <span class="comment">// 发现Person p对象也被封装成了结构体,在下边</span></span><br><span class="line">  __Block_byref_p_0 *p; <span class="comment">// 外面写的__weak,和__strong在这里没有作用,Block对p是强引用</span></span><br><span class="line">    <span class="comment">// 而在 __Block_byref_p_0里面p是对Person,根据引用类型来引用</span></span><br><span class="line">  __main_block_impl_0(<span class="keyword">void</span> *fp, struct __main_block_desc_0 *desc, __Block_byref_p_0 *_p, <span class="keyword">int</span> flags=<span class="number">0</span>) : p(_p-&gt;__forwarding) &#123;</span><br><span class="line">    impl.isa = &amp;_NSConcreteStackBlock;</span><br><span class="line">    impl.Flags = flags;</span><br><span class="line">    impl.FuncPtr = fp;</span><br><span class="line">    Desc = desc;	<span class="comment">// 根据Desc从堆中移除</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">------------------------------------------------------</span><br><span class="line"><span class="comment">// Person p 被封装的结构体</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> __<span class="title">Block_byref_p_0</span> &#123;</span></span><br><span class="line">  <span class="keyword">void</span> *__isa;  <span class="comment">//8</span></span><br><span class="line">__Block_byref_p_0 *__forwarding;  <span class="comment">//8</span></span><br><span class="line"> <span class="keyword">int</span> __flags;    <span class="comment">//4</span></span><br><span class="line"> <span class="keyword">int</span> __size;     <span class="comment">//4</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 跟 __block int age = 10;不一样的地方,这个结构体有自己的copy和dispose函数</span></span><br><span class="line"> <span class="keyword">void</span> (*__Block_byref_id_object_copy)(<span class="keyword">void</span>*, <span class="keyword">void</span>*);  <span class="comment">//8</span></span><br><span class="line"> <span class="keyword">void</span> (*__Block_byref_id_object_dispose)(<span class="keyword">void</span>*); <span class="comment">//8    正好是40个字节过后是p</span></span><br><span class="line"> Person *__strong p;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">------------------------------------------------------</span><br><span class="line"><span class="comment">// Person P结构体里面的内存管理函数</span></span><br><span class="line"><span class="comment">// 根据 上面结构体中的描述引用 Person *__strong p;</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __Block_byref_id_object_copy_131(<span class="keyword">void</span> *dst, <span class="keyword">void</span> *src) &#123;</span><br><span class="line"> _Block_object_assign((<span class="keyword">char</span>*)dst + <span class="number">40</span>, *(<span class="keyword">void</span> * *) ((<span class="keyword">char</span>*)src + <span class="number">40</span>), <span class="number">131</span>);</span><br><span class="line">    <span class="comment">//它内部有调用了 block本身Desc的copy函数 加40正好是  Person *__strong p; 的地址</span></span><br><span class="line">    </span><br><span class="line"><span class="comment">// 负责release</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __Block_byref_id_object_dispose_131(<span class="keyword">void</span> *src) &#123;</span><br><span class="line"> _Block_object_dispose(*(<span class="keyword">void</span> * *) ((<span class="keyword">char</span>*)src + <span class="number">40</span>), <span class="number">131</span>);</span><br><span class="line">    <span class="comment">//它内部有调用了 block本身Desc的dispose函数</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">------------------------------------------------------</span><br><span class="line"><span class="comment">// block调用</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_func_0(struct __main_block_impl_0 *__cself) &#123;</span><br><span class="line">  __Block_byref_p_0 *p = __cself-&gt;p; <span class="comment">// bound by ref</span></span><br><span class="line">    </span><br><span class="line">    NSLog((NSString *)&amp;__NSConstantStringImpl__var_folders_m2_qhy54krj2vb19gjw_c7rx9gc0000gp_T_main_af7190_mi_0,(p-&gt;__forwarding-&gt;p));</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">------------------------------------------------------</span><br><span class="line"><span class="comment">//block本身Desc的copy和dispose</span></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) &#123;</span><br><span class="line">    _Block_object_assign((<span class="keyword">void</span>*)&amp;dst-&gt;p, (<span class="keyword">void</span>*)src-&gt;p, <span class="number">8</span><span class="comment">/*BLOCK_FIELD_IS_BYREF*/</span>); <span class="comment">//strong</span></span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="keyword">void</span> __main_block_dispose_0(struct __main_block_impl_0*src) &#123;</span><br><span class="line">    _Block_object_dispose((<span class="keyword">void</span>*)src-&gt;p, <span class="number">8</span><span class="comment">/*BLOCK_FIELD_IS_BYREF*/</span>);</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">------------------------------------------------------</span><br><span class="line"><span class="comment">//block描述</span></span><br><span class="line"><span class="keyword">static</span> <span class="class"><span class="keyword">struct</span> __<span class="title">main_block_desc_0</span> &#123;</span></span><br><span class="line">  <span class="keyword">size_t</span> reserved;</span><br><span class="line">  <span class="keyword">size_t</span> Block_size;</span><br><span class="line">  <span class="keyword">void</span> (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);</span><br><span class="line">  <span class="keyword">void</span> (*dispose)(struct __main_block_impl_0*);</span><br><span class="line">&#125; __main_block_desc_0_DATA = &#123;</span><br><span class="line">    <span class="number">0</span>,</span><br><span class="line">    <span class="keyword">sizeof</span>(struct __main_block_impl_0)</span><br><span class="line">    , __main_block_copy_0,</span><br><span class="line">    __main_block_dispose_0</span><br><span class="line">    </span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">------------------------------------------------------</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> * argv[])</span> </span>&#123;</span><br><span class="line">    <span class="comment">/* @autoreleasepool */</span> &#123; __AtAutoreleasePool __autoreleasepool;</span><br><span class="line">        </span><br><span class="line">        __attribute__((__blocks__(byref))) __Block_byref_p_0 p = &#123;(<span class="keyword">void</span>*)<span class="number">0</span>,</span><br><span class="line">            (__Block_byref_p_0 *)&amp;p,</span><br><span class="line">            <span class="number">33554432</span>,</span><br><span class="line">            <span class="keyword">sizeof</span>(__Block_byref_p_0),</span><br><span class="line">            __Block_byref_id_object_copy_131,</span><br><span class="line">            __Block_byref_id_object_dispose_131,</span><br><span class="line">            ((Person *(*)(id, SEL))(<span class="keyword">void</span> *)objc_msgSend)((id)((Person *(*)(id, SEL))(<span class="keyword">void</span> *)objc_msgSend)((id)objc_getClass(<span class="string">"Person"</span>),</span><br><span class="line">            sel_registerName(<span class="string">"alloc"</span>)),</span><br><span class="line">            sel_registerName(<span class="string">"init"</span>))&#125;;</span><br><span class="line">        </span><br><span class="line">        Block block = ((<span class="keyword">void</span> (*)())&amp;__main_block_impl_0((<span class="keyword">void</span> *)__main_block_func_0,        	   				&amp;__main_block_desc_0_DATA,</span><br><span class="line">					(__Block_byref_p_0 *)&amp;p,</span><br><span class="line">					<span class="number">570425344</span>));</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">__block MRC环境下不会产生强引用</span><br><span class="line">__block ARC环境下根据修饰类型进行引用,比如 __block __weak Per *p = [Per new];,那么就是弱引用</span><br></pre></td></tr></table></figure>
<h5 id="ARC下循环引用"><a href="#ARC下循环引用" class="headerlink" title="ARC下循环引用"></a>ARC下循环引用</h5><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Person *person = [[Person alloc]init];</span><br><span class="line"><span class="comment">//__weak Person *weakPerson = persn;</span></span><br><span class="line">__<span class="function">weak <span class="title">typeof</span><span class="params">(person)</span>weakPerson </span>= person;</span><br><span class="line">person.block = ^&#123;</span><br><span class="line">    NSL(@<span class="string">"%d"</span>,weakPerson.age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Person *person = [[Person alloc]init];</span><br><span class="line">__<span class="function">unsafe_unretained <span class="title">typeof</span><span class="params">(person)</span>weakPerson </span>= person;</span><br><span class="line">person.block = ^&#123;</span><br><span class="line">    NSL(@<span class="string">"%d"</span>,weakPerson.age);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="MRC下"><a href="#MRC下" class="headerlink" title="MRC下"></a>MRC下</h5><p><strong>MRC不支持__weak!</strong></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// __unsafe_unretained 不会引用计数器加一</span><br><span class="line">__unsafe_unretained Person *p = [[Person alloc]init];</span><br><span class="line">        MyBolck block = ^&#123;</span><br><span class="line">            NSLog(@&quot;%d&quot;,p.age);</span><br><span class="line">        &#125;;</span><br><span class="line">        [p release];</span><br></pre></td></tr></table></figure>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">//	在MRC下__block 不会对p进行强引用</span><br><span class="line">__block Person *p = [[Person alloc]init];</span><br><span class="line">        MyBolck block = ^&#123;</span><br><span class="line">            NSLog(@&quot;%d&quot;,p.age);</span><br><span class="line">        &#125;;</span><br><span class="line">        [p release];</span><br></pre></td></tr></table></figure>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/06/15/Category分类/" rel="next" title="Category分类">
                <i class="fa fa-chevron-left"></i> Category分类
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/06/17/Alipay/" rel="prev" title="Alipay">
                Alipay <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/10.png"
                alt="Erhqe" />
            
              <p class="site-author-name" itemprop="name">Erhqe</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">70</span>
                  <span class="site-state-item-name">posts</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">categories</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">tags</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-5"><a class="nav-link" href="#变量捕获"><span class="nav-number">1.</span> <span class="nav-text">变量捕获</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Block类型"><span class="nav-number">2.</span> <span class="nav-text">Block类型</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Block基本数据类型"><span class="nav-number">3.</span> <span class="nav-text">__Block基本数据类型</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#Block对象类型"><span class="nav-number">4.</span> <span class="nav-text">__Block对象类型</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#ARC下循环引用"><span class="nav-number">5.</span> <span class="nav-text">ARC下循环引用</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#MRC下"><span class="nav-number">6.</span> <span class="nav-text">MRC下</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Erhqe</span>

  
</div>


  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a></div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'TiAY2J24ECwQH9uLYsoJpgIK-gzGzoHsz',
        appKey: 'Y9PiVrPXVmfNsv4Aa6cPsADG',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  

  

  

  

</body>
</html>
